package com.example.linktop.broadcastbestpractice;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by linktoplinktop on 2017/7/4.
 */
public class BaseActivity extends AppCompatActivity {


    private OfflineReceiver oflineRecv;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityController.addActivity(this);
        oflineRecv = new OfflineReceiver();
    }


    @Override
    protected void onResume() {
        super.onResume();

        //添加监听器
        IntentFilter filter = new IntentFilter();
        filter.addAction("BROASCAST_OFFLINE");
        registerReceiver(oflineRecv, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(oflineRecv);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityController.removeActivity(this);

        //移除通知

    }

    class OfflineReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(final Context context, Intent intent) {

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
            alertDialog.setCancelable(false);
            alertDialog.setMessage("要下线吗?");
            alertDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //强制下限
                    ActivityController.finishAll();
                    Intent intent = new Intent(context, LoginActivity.class);
                    startActivity(intent); //重启登录界面

                }
            });

            alertDialog.show();

        }
    }


}
